<template>
  <div class="h-container">
    <h2>{{ component.__sourceCodeTitle }}</h2>
    <p v-html="component.__sourceDescription"></p>
    <div class="h-component-container">
      <div class="h-component-sample">
        <div>
          <!-- 使用 component 组件做小优化 -->
          <component :is="component" />
        </div>
      </div>
      <div class="h-code-container">
        <div class="h-code-wrapper" :class="{ 'h-code-visible': toggle }">
           <pre class="language-html" v-html="html"></pre>
        </div>
        <a class="h-openOrHide" @click="codeVisible">
          {{ toggle ? '隐藏代码' : '显示代码' }}
        </a>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import "prismjs"
import "prismjs/themes/prism.css"
import { ref, computed } from 'vue'

const Prism = (window as any).Prism
export default {
  props: {
    component: Object,
  },
  setup(props) {
    const toggle = ref(false)
    const codeVisible = (val: String) => {
      toggle.value = !toggle.value
    }
    const html = computed(() => {
      let sourceCode = props.component.__sourceCode
      sourceCode = sourceCode.replace('../lib/index', 'haha-ui')
      return Prism.highlight(sourceCode, Prism.languages.html, 'html')
    })
    return {
      Prism,
      toggle,
      codeVisible,
      html,
    }
  },
}
</script>

<style lang="scss" scoped>
$border-color: #c5d9e8;
.h-container {
  margin: 16px 0 32px;
  > h2 {
    padding: 8px 16px;
    // border-bottom: 1px solid $border-color;
  }
  &-component {
    padding: 16px;
  }
  &-actions {
    padding: 8px 16px;
    border-top: 1px dashed $border-color;
  }
  &-code {
    padding: 8px 16px;
    border-top: 1px dashed $border-color;
    > pre {
      line-height: 1.1;
      font-family: Consolas, 'Courier New', Courier, monospace;
      margin: 0;
    }
  }
}
</style>
